<?php
require_once 'Utils.php';
require_once 'OAuthResponse.php';
require_once 'OAuthServerBase.php';


/**
 * Demo REST server expose public method
 *
 * @author: Phong Linh <langxangvn@gmail.com>
*/
class MathServer extends OAuthServerBase {
	
	/**
	 * This is variable PARAMETER_A_NOT_NUMERIC description
	 *
	 */
	public static $ERR_A_NOT_NUMERIC = 1001;
	
	/**
	* This is variable PARAMETER_A_NOT_NUMERIC description
	*
	*/
	public static $ERR_B_NOT_NUMERIC = 1002;
	
	/**
	* This is variable PARAMETER_A_NOT_NUMERIC description
	*
	*/
	public static $DIVISION_BY_ZERO = 1003;
	
	/**
	 * The data store
	 *
	 * @var OAuthStore 
	 *
	 */
	protected $_forceBypassNonce = true;
	
	/**
	 * Override get error
	 *
	 * @param mixed $code This is a description
	 * @return mixed This is the return value description
	 *
	 */
	public function getError($code) {
		try {
			return parent::getError($code);
		} catch(Exception $e) {
			if(self::$ERR_A_NOT_NUMERIC == (int)$code) {
				return array('name' => 'Wrong A parameter', 
					'msg' => 'Parameter A is not numeric.');
			} elseif(self::$ERR_B_NOT_NUMERIC == (int)$code) {
				return array('name' => 'Wrong B parameter', 
					'msg' => 'Parameter B is not numeric.');
			} elseif(self::$DIVISION_BY_ZERO == (int)$code) {
				return array('name' => 'Division by zero', 
					'msg' => 'Division by zero exception.');
			} else {
				
			}
			
			throw new Exception('Not defined error.');
		}
	}
	
	
	/**
	 * This is method handle
	 *
	 * @return mixed This is the return value description
	 *
	 */
	public function handle($action, $params = array()) {	
		switch ($action) {
			case 'add':
				$result = $this->add();
				break;
			
			case 'subtract':
				$result = $this->subtract();
				break;
			
			case 'multiply':
				$result = $this->multiply();
				break;
			
			case 'divide':
				$result = $this->divide();
				break;								
			
			default:
				$code = self::$UNSUPPORTED_METHOD;
				$error = $this->getErrorMessage($code);
				throw new OAuthException($error, $code);
		}
		
		return $result;
	}
	
	
	/**
	 * This is method add
	 *
	 * @return mixed This is the return value description
	 *
	 */
	public function add() {
		$this->verify(false, $this->_forceBypassNonce);
		
		$a = $this->getParam('a', true);
		$b = $this->getParam('b', true);
		
		if(!is_numeric($a)) {
			$code = self::$ERR_A_NOT_NUMERIC;
			$error = $this->getErrorMessage($code);
			throw new OAuthException($error, $code);			
		} 
		
		if(!is_numeric($b)) {
			$code = self::$ERR_B_NOT_NUMERIC;
			$error = $this->getErrorMessage($code);
			throw new OAuthException($error, $code);
		} 		
		
		return ($a + $b);
	}
	
	/**
	 * This is method subtract
	 *
	 * @return mixed This is the return value description
	 *
	 */
	public function subtract() {
		$this->verify(false);
		
		$a = $this->getParam('a', true);
		$b = $this->getParam('b', true);
		
		if(!is_numeric($a)) {
			$code = self::$ERR_A_NOT_NUMERIC;
			$error = $this->getErrorMessage($code);
			throw new OAuthException($error, $code);			
		} 
		
		if(!is_numeric($b)) {
			$code = self::$ERR_B_NOT_NUMERIC;
			$error = $this->getErrorMessage($code);
			throw new OAuthException($error, $code);			
		} 
		
		return ($a - $b);
	}	
	
	/**
	 * This is method multiply
	 *
	 * @return mixed This is the return value description
	 *
	 */
	public function multiply() {
		$this->verify(false);
		
		$a = $this->getParam('a', true);
		$b = $this->getParam('b', true);
		
		if(!is_numeric($a)) {
			$code = self::$ERR_A_NOT_NUMERIC;
			$error = $this->getErrorMessage($code);
			throw new OAuthException($error, $code);			
		} 
		
		if(!is_numeric($b)) {
			$code = self::$ERR_B_NOT_NUMERIC;
			$error = $this->getErrorMessage($code);
			throw new OAuthException($error, $code);			
		} 		
		
		return ($a * $b);
	}
	
	/**
	 * This is method divide
	 *
	 * @return mixed This is the return value description
	 *
	 */
	public function divide() {
		$this->verify(false);
		
		$a = $this->getParam('a', true);
		$b = $this->getParam('b', true);
		
		if(!is_numeric($a)) {
			$code = self::$ERR_A_NOT_NUMERIC;
			$error = $this->getErrorMessage($code);
			throw new OAuthException($error, $code);			
		} 
		
		if(!is_numeric($b)) {
			$code = self::$ERR_B_NOT_NUMERIC;
			$error = $this->getErrorMessage($code);
			throw new OAuthException($error, $code);			
		} 		
		
		try {
			$result = ($a / $b);
			if(!is_numeric($result)) {
				throw new Exception('Division by zero');
			}
			
			return $result;
		} catch(Exception $e) {
			$code = self::$DIVISION_BY_ZERO;
			$error = $this->getErrorMessage($code);
			throw new OAuthException($error, $code);	
		}		
	}	
}

?>